﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;

namespace Chambills.Present.Validation.Sample.WPF
{
    public class PersonViewModel : ValidatingViewModel
    {
        private string firstname;
        private string lastname;
        private int age;

        public PersonViewModel() : base()
        {
            SaveCommand = new DelegateCommand<object>(x => Save());

        }

       

        private void Save()
        {
            Validator.Enabled = true;
            MessageBox.Show(!IsValid ? Error : "Saved!");
        }

        public ICommand SaveCommand { get; private set; }

        [Required(ErrorMessage = "FirstName is Required.")]
        public string FirstName
        {
            get { return firstname; }
            set
            {
                firstname = value;
                OnPropertyChanged(() => FirstName);
            }
        }

        [Required(ErrorMessage = "LastName is Required.")]
        public string LastName
        {
            get { return lastname; }
            set
            {
                lastname = value;
                OnPropertyChanged(() => LastName);
            }
        }

        [Range(0, 120, ErrorMessage = "Age must be between 0 and 120")]
        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                OnPropertyChanged(() => Age);
            }
        }
    }
}
